<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Event_entity{
	var $id = 0;
	var $name = "";
	var $team_id = 0;
	var $match_id = 0;
	var $status = "";
	var $event_category_id = 0;
	var $start_time = 0;
	var $end_time = 0;
	var $bet_enable = "";
	var $odds = 0;
	var $is_root = "";
	var $result = "";
	var $ordering = 0;
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_EVENT)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->name = $result[0]->name;
			$this->team_id = $result[0]->team_id;
			$this->match_id = $result[0]->match_id;
			$this->status = $result[0]->status;
			$this->event_category_id = $result[0]->event_category_id;
			$this->start_time = $result[0]->start_time;
			$this->end_time = $result[0]->end_time;
			$this->bet_enable = $result[0]->bet_enable;
			$this->odds = $result[0]->odds;
			$this->is_root = $result[0]->is_root;
			$this->result = $result[0]->result;
			$this->ordering = $result[0]->ordering;
			
			return $this;
		}
		return false;
	}
	
	function setName($input){ $this->name=$input; }
	function setTeamId($input){ $this->team_id=$input; }
	function setMatchId($input){ $this->match_id=$input;}
	function setStatus($input){ $this->status=$input; }
	function setEventCategoryId($input){ $this->event_category_id=$input; }
	function setStartTime($input){ $this->start_time=$input; }
	function setEndTime($input){ $this->end_time=$input; }
	function setBetEnable($input){ $this->bet_enable=$input; }
	function setOdds($input){ $this->odds=$input; }
	function setIsRoot($input){ $this->is_root=$input; }
	function setResult($input){ $this->result=$input; }
	function setOrdering($input){ $this->ordering=$input; }
	
	function getId(){ return $this->id; }
	function getTeamId(){ return $this->team_id;}
	function getMatchId(){ return $this->match_id; }
	function getName(){ return $this->name; }
	function getStatus(){ return $this->status; }
	function getEventCategoryId(){ return $this->event_category_id; }
	function getStartTime(){ return $this->start_time; }
	function getEndTime(){ return $this->end_time; }
	function getBetEnable(){ return $this->bet_enable; }
	function getOdds(){ return $this->odds; }
	function getIsRoot(){ return $this->is_root; }
	function getResult(){ return $this->result; } 
	function getOrdering(){ return $this->ordering; }
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('name', $this->name);
		$ci->db->set('team_id', $this->team_id);
		$ci->db->set('match_id', $this->match_id);
		$ci->db->set('status', $this->status);
		$ci->db->set('event_category_id', $this->event_category_id);
		$ci->db->set('start_time', $this->start_time);
		$ci->db->set('end_time', $this->end_time);
		$ci->db->set('bet_enable', $this->bet_enable);
		$ci->db->set('odds', $this->odds);
		$ci->db->set('is_root', $this->is_root);
		$ci->db->set('result', $this->result);
		$ci->db->set('ordering', $this->ordering);
		
		$ci->db->where('id', $this->id)->update(TBL_EVENT);
	}
	
	function createNewEventEntity(){
		$ci=& get_instance();
		$ci->db->set('name', '');
		$ci->db->insert(TBL_EVENT);
		
		return $ci->db->insert_id();
	}
	
	function findDrawEventOfMatch($match_id){
		$ci=& get_instance();
		return $ci->db->where("name LIKE 'Draw' AND match_id =$match_id")->get(TBL_EVENT)->result();
	}
	
	function getArrayListOfEvent($event_category_id){
		$ci=& get_instance();
		return $ci->db->where('event_category_id', $event_category_id)->order_by('ordering','ASC')->get(TBL_EVENT)->result();
	}
	
	function getLivingEventOfCategory($event_category_id){
		$ci=& get_instance();
		return $ci->db->where( array('event_category_id'=> $event_category_id,'status'=>'living'))->order_by('ordering','ASC')->get(TBL_EVENT)->result();
	}
	
	function getLivingEventOfMatch($match_id){
		$ci=& get_instance();
		return $ci->db->where( array('match_id'=> $match_id,'status'=>'living'))->get(TBL_EVENT)->result();
	}

// deprecated	
//	function getLivingEventOfTournament($tournament_id){
//		$ci=& get_instance();
//		return $ci->db->where( array('tournament_id'=> $tournament_id,'status'=>'living'))->get(TBL_EVENT)->result();
//	}
	
	function getWonTeamOfMatch($match_id){
		$ci=& get_instance();
		$array = $ci->db->where( array('match_id'=> $match_id,'status'=>'closed','is_root'=>'yes'))->get(TBL_EVENT)->result();
		$team_id = 0;
		foreach($array as $item){
			if($item->result == 'won' AND $item->team_id != 0){
				$team_id = $item->team_id;
				break;
			}
		}
		return $team_id;
	}
	
	function changeStatusEventRealTime(){
		//$ci=& get_instance();
		// do nothing here
		//$ci->db->where("status LIKE 'living' AND result IS NOT NULL AND end_time <=".time())->update(TBL_EVENT, array('status'=>'closed'));					
	}
}
